export default {
	data() {
		return {
			title: '充值',
			type: 'topUp',
			goodsData: {},
			orderData: {
				pay_money: 0,
				goods_list: [],
				remark: '',
				goods_num: 0,
				card_type: 'membercard',
				create_time: 0
			},
			isRepeat: false,
			actionBtnShwo: true,
			pageActive: '' // 记录当前页面活跃值
		};
	},
	onLoad(option) {
		uni.hideTabBar();
		uni.setStorageSync('orderData', {
			pay_money: 0,
			goods_list: [],
			remark: '',
			goods_num: 0,
			card_type: 'membercard'
		});
	},
	onShow() {
		this.type = 'topUp';
		var pages = getCurrentPages(); // 获取栈实例
		let currentPage = pages[pages.length - 1]['$page']['fullPath'];
		this.orderData = uni.getStorageSync('orderData');
		this.orderData.create_time = this.$util.timeFormat(parseInt(new Date().getTime() / 1000));
		this.addKeyDownEvent();
	},
	onHide() {
		this.removeKeyDownEvent();
	},
	methods: {
		openSelectMember() {
			if (this.$refs.selectMember) {
				this.pageActive = 'ShowMember';
				this.$refs.selectMember.open(() => {
					this.pageActive = 'ShowMemberAfter';
				});
				setTimeout(() => {
					this.$refs.selectMember.inputFocus = true;
				}, 200);
			}
		},
		showMember() {
			this.pageActive = 'ShowMember';
			if (!this.memberInfo || !this.memberInfo.member_id) {
				this.openSelectMember();
			} else {
				this.type = 'member';
				this.pageActive = 'ShowMemberAfter';
			}
		},
		clearGoods(data) {
			this.goodsData = [];
		},
		// 控制操作按钮是否显示
		actionBtnShwoFn(data) {
			this.actionBtnShwo = data;
		},
		/**
		 * 添加键盘监听事件
		 */
		addKeyDownEvent() {
			// #ifdef H5
			// 绑定监听事件
			window.addEventListener("keydown", this.listenerKeyDown, true);

			// 监听F1~F12，BACKSPACE
			window.POS_HOTKEY_CALLBACK = (control, code) => {
				this.posHotKeyCallback(code);
			};
			// #endif
		},
		/**
		 * 移除键盘监听事件
		 */
		removeKeyDownEvent() {
			// #ifdef H5
			window.removeEventListener("keydown", this.listenerKeyDown, true);

			delete window.POS_HOTKEY_CALLBACK;
			// #endif
		},
		listenerKeyDown(e) {
			var code = e.code;

			// console.log('KeyDown', this.type, code, this.pageActive, this.orderData, e);

			// 快捷支付，KeyN：付款码支付，KeyC：现金支付（cash），KeyW：微信支付（wechat），A：支付宝支付（alipay），KeyP：POS刷卡支付（POS）
			let letterCode = ['KeyN', 'KeyC', 'KeyW', 'KeyA', 'KeyP'];

			if (this.pageActive == '' && code == 'KeyM') {
				// 选择会员，键盘快捷键【M】

				this.openSelectMember();

			} else if (this.pageActive == 'ShowMember') {
				// 打开会员弹出框

				this.memberCallback(code);

			} else if (this.pageActive == 'ShowMemberAfter') {
				// 活跃窗口：设置会员后

				if (code == 'Enter' || code == 'NumpadEnter') {
					this.$refs.member.pay(() => {
						this.pageActive = 'OrderCreate';
					});
				}

			} else if (this.type == 'topUp' && this.pageActive == '') {
				// 选择充值金额

				if (code == 'Enter' || code == 'NumpadEnter') {
					this.$refs.member.pay(() => {
						this.pageActive = 'OrderCreate';
					});
				}

			} else if (this.pageActive == 'OrderCreate') {
				// 创建订单

				if (letterCode.indexOf(code) != -1) {
					// 快捷下单

					this.quickOrderCallback(code);

				} else {
					this.orderCreateCallback(code);
				}

			}

		},
		/**
		 * 监听键盘事件回调
		 * @param {Object} code
		 */
		posHotKeyCallback(code) {
			if (code == 'F2') {
				// 选择充值
				this.type = 'topUp';
				this.pageActive = '';
			} else if (code == 'F3') {
				// 选择会员
				this.showMember();
			} else if (code == 'BACKSPACE') {
				// 退格键
				if (this.pageActive == 'OrderCreate') {
					if (this.$refs.member && this.$refs.member.$refs.payment) {
						var payment = this.$refs.member.$refs.payment;
						if (payment.active == 'openMoneyPopup') {
							payment.deleteCode();
						}
					}
				}

			}  else {
				// 触发左侧菜单按键回调
				this.menuTriggerKeyCodeCallBack(code);
			}
		},
		/**
		 * 打开会员弹出框 事件回调
		 * @param {string} code 按键代码
		 */
		memberCallback(code) {
			let selectMember = this.$refs.selectMember;

			// 选择会员，回车确认
			if (code == 'Enter' || code == 'NumpadEnter') {
				if (selectMember && selectMember.memberId && selectMember.active == 'SearchMember') {
					selectMember.getMemberInfo(selectMember.memberId, () => {
						this.pageActive = 'ShowMemberAfter';
					});
				}
			}
		},
		/**
		 * 快捷下单 事件回调
		 * @param {string} code 按键代码
		 */
		quickOrderCallback(code) {

			if (!(this.$refs.member && this.$refs.member.$refs.payment)) return;

			let member = this.$refs.member;
			let payment = member.$refs.payment;

			// 不能存在操作
			if (payment.active) return;

			// 快捷支付，KeyN：付款码支付，KeyC：现金支付（cash），KeyW：微信支付（wechat），A：支付宝支付（alipay），KeyP：POS刷卡支付（POS）
			payment.hotKeyPay(code, this.type, () => {
				member.pay(() => {
					payment.hotKeyPayCallback(code);
				});
			});

		},
		/**
		 * 订单创建 事件回调
		 * @param {string} code 按键代码
		 */
		orderCreateCallback(code) {

			if (!(this.$refs.member && this.$refs.member.$refs.payment)) return;

			let member = this.$refs.member;
			let payment = member.$refs.payment;

			if (code == 'Escape') {
				// 取消支付，回到上一步，检测当前没有弹出框

				if (payment.active == '') {

					payment.cancelPayment();
					member.pageActive = '';

				}

			} else if (payment.active == 'OpenMoneyPopup') {

				if (code == 'Enter' || code == 'NumpadEnter') {

					if (payment.moneyPopup.type == 'reduction') {
						// 减免金额

						payment.moneyPopupConfirm();

					} else if (payment.moneyPopup.type == 'cash') {
						// 现金支付

						payment.moneyPopupConfirm(() => {
							payment.confirm();
						});

					}

				} else if (code == 'NumpadDecimal') {

					payment.keydown('.');

				} else if (code.indexOf('Numpad') != -1) {

					var num = code.replace('Numpad', '')
					payment.keydown(num);


				} else if (code.indexOf('Digit') != -1) {

					var num = code.replace('Digit', '')
					payment.keydown(num);

				}

			} else if (code == 'Enter' || code == 'NumpadEnter') {

				if (payment.paystatus == 'success') {
					// 支付成功

					payment.paySuccess();
					this.pageActive = '';

				} else if (payment.active == 'RemarkPopup') {
					// 备注

					payment.remarkConfirm();

				} else if (payment.active == 'safeVerifyPopup') {
					// 使用付款码，出示付款码、验证手机号

					setTimeout(() => {
						// 打开付款码，设置焦点
						payment.scancodeFocus = true;
					}, 200);

					if (payment.safeVerifyType == 'payment_code') {

						payment.verifyPaymentCode({
							detail: {
								value: payment.paymentCode
							}
						});

					} else if (payment.safeVerifyType == 'sms_code') {

						payment.verifySmsCode({
							detail: {
								value: payment.smsCode
							}
						});

					}

				} else if (payment.active == 'thirdConfirm') {
					// 打开付款码，设置焦点

					setTimeout(() => {
						// 打开付款码，设置焦点
						payment.scancodeFocus = true;
					}, 200);

				} else if (payment.type == 'cash') {

					// 选择【现金支付】方式，打开现金支付弹出框
					if (payment.active == '') {
						payment.switchPayType(payment.type);
					}

				} else if (payment.type != 'third') {
					payment.confirm();
				} else if (payment.active == '') {
					// 付款码/扫码枪

					payment.thirdConfirm();
					setTimeout(() => {
						// 初次打开，设置焦点
						payment.scancodeFocus = true;
					}, 200);

				}
			}
		}
	},
	watch: {
		type: function(nVal) {
			if (nVal != 'member') {
				this.$refs.member.search = false;
			}
		}
	}
}